package businesslogic.clientbl;

import java.util.ArrayList;

import data.clientdata.ClientDataController;
import dataservice.clientdataservice.ClientDataService;
import po.ClientPO;
import vo.ClientVO;

public class Client {
	ArrayList<ClientPO> clients;
	
	public Client(){
		super();
		ClientDataService controller = new ClientDataController();
		clients = controller.getClientList();
	}

	public ArrayList<ClientPO> getClientPOList() {
		return clients;
	}
	
	public ArrayList<ClientVO> getClientVOList(){
		ArrayList<ClientVO> vos = new ArrayList<ClientVO>();
		for (int i = 0; i < clients.size(); i++) {
			ClientVO vo = new ClientVO();
			transform_PO_to_VO(vo, clients.get(i));
			vos.add(vo);
		}
		
		return vos;
	}
	public boolean addClient(ClientVO vo) {
		// TODO Auto-generated method stub
		if (!Client.isExist(vo)) {
			ClientPO po = new ClientPO();
			Client.transform_VO_to_PO(po, vo);
			ClientDataService clientData = new ClientDataController();
			clientData.Insert(po);
			return true;
		}
		return false;
	}

	public boolean deleteClient(String num) {
		// TODO Auto-generated method stub
		ClientVO vo = findClientByNum(num);
		if (vo == null) {
			return false;
		}
		//该用户还有应收或者应付
		if ((vo.getPayable() != 0) || (vo.getReceivable() != 0)) {
			return false;
		}
		ClientDataService clientData = new ClientDataController();
		clientData.delete(num);
		return true;
	}

	public boolean changeClient(ClientVO vo) {
		// TODO Auto-generated method stub
		if (Client.isExist(vo)) {
			//该用户存在
			ClientDataService clientData = new ClientDataController();
			ClientPO po = new ClientPO();
			Client.transform_VO_to_PO(po, vo);
			clientData.update(po);
			return true;
		}
		return false;
	}

	public ClientVO findClientByNum(String num) {
		// TODO Auto-generated method stub
		ClientDataService clientData = new ClientDataController();
		ClientPO po = clientData.findByNum(num);
		if (po == null) {
			return null;
		}
		
		ClientVO vo = new ClientVO();
		Client.transform_PO_to_VO(vo, po);
		return vo;
	}

	public ArrayList<ClientVO> findCientByKey(String key) {
		// TODO Auto-generated method stub
		ClientDataService clientData = new ClientDataController();
		ArrayList<ClientPO> po = clientData.findByKey(key);
		ArrayList<ClientVO> vos = new ArrayList<ClientVO>();
		if (po == null) {
			return null;
		}
		for (int i = 0; i < po.size(); i++) {
			ClientVO vo = new ClientVO();
			transform_PO_to_VO(vo, po.get(i));
			vos.add(vo);
		}
		return vos;
	}

	public ClientVO getMessage() {
		// TODO Auto-generated method stub
		return null;
	}
	
	public ArrayList<ClientVO> getClientList() {
		// TODO Auto-generated method stub
		return null;
	}
	
	
	public boolean changeReceivable(String clientNum, double price){
		return true;
	}
	
	public boolean changePayable(String clientNum, double price){
		return true;
	}
	
	public boolean changeQuota(String clientNum, double quota){
		return true;
	}
	
	static void transform_PO_to_VO(ClientVO vo, ClientPO po){
		vo.setAddress(po.getAddress());
		vo.setClassification(po.getClassification());
		vo.setDefaultSalesman(po.getDefaultSalesman());
		vo.setEmail(po.getEmail());
		vo.setLevel(po.getLevel());
		vo.setName(po.getName());
		vo.setNum(po.getNum());
		vo.setPayable(po.getPayable());
		vo.setPostCode(po.getPostCode());
		vo.setQuota(po.getQuota());
		vo.setReceivable(po.getReceivable());
		vo.setTelephone(po.getTelephone());
	}
	
	static void transform_VO_to_PO(ClientPO po, ClientVO vo){
		po.setAddress(vo.getAddress());
		po.setClassification(vo.getClassification());
		po.setDefaultSalesman(vo.getDefaultSalesman());
		po.setEmail(vo.getEmail());
		po.setLevel(vo.getLevel());
		po.setName(vo.getName());
		po.setNum(vo.getNum());
		po.setPayable(vo.getPayable());
		po.setPostCode(vo.getPostCode());
		po.setQuota(vo.getQuota());
		po.setReceivable(vo.getReceivable());
		po.setTelephone(vo.getTelephone());
	}
	
	//判断客户是否存在
	static boolean isExist(ClientVO vo){
		ClientDataService clientData = new ClientDataController();
		if (clientData.findByNum(vo.getNum()) != null) {
			return true;
		}
		
		return false;
	}
	
}
